package com.offcn.dongyimai.sellergoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.dongyimai.common.utils.PageResult;
import com.offcn.dongyimai.sellergoods.mapper.SpecificationMapper;
import com.offcn.dongyimai.sellergoods.mapper.SpecificationOptionMapper;
import com.offcn.dongyimai.sellergoods.pojo.Specification;
import com.offcn.dongyimai.sellergoods.pojo.SpecificationOption;
import com.offcn.dongyimai.sellergoods.pojo.vo.SpecificationVo;
import com.offcn.dongyimai.sellergoods.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/****
 * @Author:ujiuye
 * @Description:Specification业务层接口实现类
 * @Date 2021/2/1 14:19
 *****/
@Service
public class SpecificationServiceImpl extends ServiceImpl<SpecificationMapper, Specification> implements SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;

    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    /**
     * Specification条件+分页查询
     *
     * @param specification 查询条件
     * @param page          页码
     * @param size          页大小
     * @return 分页结果
     */
    @Override
    public PageResult<Specification> findPage(Specification specification, int page, int size) {
        Page<Specification> mypage = new Page<>(page, size);
        QueryWrapper<Specification> queryWrapper = this.createQueryWrapper(specification);
        IPage<Specification> iPage = this.page(mypage, queryWrapper);
        return new PageResult<Specification>(iPage.getTotal(), iPage.getRecords());
    }

    /**
     * Specification分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Specification> findPage(int page, int size) {
        Page<Specification> mypage = new Page<>(page, size);
        IPage<Specification> iPage = this.page(mypage, new QueryWrapper<Specification>());

        return new PageResult<Specification>(iPage.getTotal(), iPage.getRecords());
    }

    /**
     * Specification条件查询
     *
     * @param specification
     * @return
     */
    @Override
    public List<Specification> findList(Specification specification) {
        //构建查询条件
        QueryWrapper<Specification> queryWrapper = this.createQueryWrapper(specification);
        //根据构建的条件查询数据
        return this.list(queryWrapper);
    }


    /**
     * Specification构建查询对象
     *
     * @param specification
     * @return
     */
    public QueryWrapper<Specification> createQueryWrapper(Specification specification) {
        QueryWrapper<Specification> queryWrapper = new QueryWrapper<>();
        if (specification != null) {
            // 主键
            if (!StringUtils.isEmpty(specification.getId())) {
                queryWrapper.eq("id", specification.getId());
            }
            // 名称
            if (!StringUtils.isEmpty(specification.getSpecName())) {
                queryWrapper.eq("spec_name", specification.getSpecName());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        this.removeById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("spec_id",id);
        specificationOptionMapper.delete(queryWrapper);
    }

    /**
     * 修改Specification
     *
     * @param specificationVo
     */
    @Override
    public void update(SpecificationVo specificationVo) {
        this.updateById(specificationVo.getSpecification());
        // 1、构建查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        // 2、根据Specification的Id查询对应的规格选项
        queryWrapper.eq("spec_id", specificationVo.getSpecification().getId());
        // 3、执行删除
        specificationOptionMapper.delete(queryWrapper);
        // 4、判断SpecificationOptionList是否为空
        if (!CollectionUtils.isEmpty(specificationVo.getSpecificationOptionList())){
            // 5、重新插入规格选项
            for (SpecificationOption specificationOption : specificationVo.getSpecificationOptionList()) {
                specificationOption.setSpecId(specificationVo.getSpecification().getId());
                specificationOptionMapper.insert(specificationOption);
            }
        }
    }

    /**
     * 增加Specification
     *
     * @param specificationVo
     */
    @Override
    public void add(SpecificationVo specificationVo) {

        this.save(specificationVo.getSpecification());
        // 1、判断规格id是否为空
        if (null != specificationVo.getSpecification().getId()
                && specificationVo.getSpecificationOptionList().size() > 0) {
            // 2、遍历规格选项
            for (SpecificationOption specificationOption : specificationVo.getSpecificationOptionList()) {
                // 3、将规格id设置到规格选项中
                specificationOption.setSpecId(specificationVo.getSpecification().getId());
                // 4、保存规格选项
                specificationOptionMapper.insert(specificationOption);
            }

        }
    }

    /**
     * 根据ID查询Specification
     *
     * @param id
     * @return
     */
    @Override
    public Specification findById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询Specification全部数据
     *
     * @return
     */
    @Override
    public List<Specification> findAll() {
        return this.list(new QueryWrapper<Specification>());
    }

    /**
     * 查询关联规格模板数据
     */
    @Override
    public List<Map> selectOptions() {
        return specificationMapper.selectOptions();
    }
}
